import argparse, gzip, matplotlib, matplotlib.pyplot as plt
import numpy as np, pickle, sys
from tqdm import *
import scipy
np.seterr(divide='ignore', invalid='ignore')
def t_test(n, url_trace, trace_name):
    fd =  open(url_trace + trace_name + "0","rb")
    arr = pickle.load( fd )
    arr = np.array(arr)
    count = 0
    N = arr.shape[1]
    old_var_even = np.zeros(N)
    old_mean_even = np.zeros(N)
    old_var_odd = np.zeros(N)
    old_mean_odd = np.zeros(N)
    oddcount = 0
    evencount = 0
    for j in trange(n):
        fd =  open(url_trace + trace_name + "{0}".format(j),"rb")
        arr = pickle.load( fd )
        arr = np.array(arr)
        flag = pickle.load( fd )
        flag = np.array(flag)
        for i in range(arr.shape[0]):
            if flag[i] == 0:
                new_mean = old_mean_even + (arr[i] - old_mean_even) / (evencount + 1)
                new_var = old_var_even + ((arr[i] - old_mean_even) * (arr[i] - new_mean) - old_var_even) / (
                        evencount + 1)
                old_mean_even = new_mean
                old_var_even = new_var
                evencount += 1
                count = count + 1
            elif flag[i] == 1:
                new_mean = old_mean_odd + (arr[i] - old_mean_odd) / (oddcount + 1)
                new_var = old_var_odd + ((arr[i] - old_mean_odd) * (arr[i] - new_mean) - old_var_odd) / (oddcount + 1)
                old_mean_odd = new_mean
                old_var_odd = new_var
                oddcount += 1
                count = count + 1
    print(count,oddcount,evencount)
    temp1 = old_mean_even - old_mean_odd
    temp2 = (old_var_even / evencount) + (old_var_odd / oddcount)
    test_result = temp1 / np.sqrt(temp2)
    return test_result

def ttest_function(num_file_blocks, url_trace, trace_name):
    plt.rcParams['figure.figsize'] = (12.0, 8.0)
    f, ax = plt.subplots(1, 1)
    ax.set_title('T-test result',fontsize = 30)
    plt.xlabel('Trace sample',fontsize = 30)
    plt.ylabel('T value',fontsize = 30)
    ax.axhline(y=4.5, ls='--', c='red', linewidth=2)
    ax.axhline(y=-4.5, ls='--', c='red', linewidth=2)
    result = t_test(num_file_blocks, url_trace, trace_name)
    np.save(url_trace + "result-ttest.npy", result)
    ax.plot(result[0:])
    plt.show()


if __name__ == '__main__':

    # 文件路径
    url_trace = r"E:\CBM\traces\base-core-overwrite/"
    # 文件数量
    num_file_blocks = 200
    # 文件名字
    trace_name = "tr-sw-base-batch-"
    # 运行函数
    ttest_function(num_file_blocks, url_trace, trace_name)

